import { _decorator, Component, Node, CylinderCollider, ICollisionEvent, Vec3, math } from "cc";
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = other
 * DateTime = Thu Mar 03 2022 23:18:25 GMT+0800 (中国标准时间)
 * Author = zxq6635624
 * FileBasename = other.ts
 * FileBasenameNoExtension = other
 * URL = db://assets/script/other.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass("other")
export class other extends Component {
  // [1]
  // dummy = '';

  // [2]
  // @property
  // serializableDummy = 0;.
  @property
  keydown_w = false;
  @property
  keydown_s = false;
  @property
  keydown_a = false;
  @property
  keydown_d = false;

  start() {
    // [3]
    let Collider = this.node.getComponent(CylinderCollider);
    Collider.on("onCollisionEnter", this.onCollision, this);
  }
  //碰撞检测
  private onCollision(event: ICollisionEvent) {
    console.log(event.type, event);
  }

  getDirection(x: number, y: number, z: number) {
    let ret = new Vec3(x, y, z);
    math.Vec3.transformQuat(ret, ret, this.node.getRotation());
    return ret;
  }

  update(deltaTime: number) {
    // [4]
    if (this.keydown_w) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(0, 0, 1), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (this.keydown_s) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(0, 0, -1), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (this.keydown_a) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(1, 0, 0), deltaTime * 5);
      this.node.setPosition(pos);
    }
    if (this.keydown_d) {
      let pos = this.node.getPosition();
      math.Vec3.scaleAndAdd(pos, pos, this.getDirection(-1, 0, 0), deltaTime * 5);
      this.node.setPosition(pos);
    }
  }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
